//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    cardData:{
      biz:"MzA4MDQxOTIxMQ==",
      encrypt_card_id:"dD9satX/U+kHLcj0B674joRlwDwRnjc+6crGXPus4K1aHbYpBaYTLo9MF0yt6DzX",
      outer_str:"Z0FXR0"
    }
  },
  onShow: function () {
    var that = this;
    that.requestCardList();
    app.setCartnum();//更新购物车数量
  },
  requestCardList: function (cid) {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_card_coupons_list';
    data.page = page;
    app.setVersion(that);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.cardList ? that.data.cardList : '';
          var curArr = res.data.data.list;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.list;
          }
          that.setData({
            cardList: allArr,
            cardCover: res.data.data.background
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.cardList);
        } else {
          if (page <= 0) {
            that.setData({
              cardList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestCardList();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestCardList();
    }
  },
  getCard:function(e){
    var cardid = e.currentTarget.dataset.cardid;
    var timestamp = e.currentTarget.dataset.timestamp;
    var signature = e.currentTarget.dataset.signature;
    var nonce_str = e.currentTarget.dataset.nonce_str;
    var cardext = JSON.stringify({ "timestamp": timestamp, "nonce_str": nonce_str, "signature": signature });
    console.log(cardid);
    console.log(cardext);
    console.log(nonce_str);
    wx.addCard({
      cardList: [
        {
          cardId: cardid,
          cardExt: cardext
        }
      ],
      success: function (res) {
        console.log(res.cardList) // 卡券添加结果
        // wx.openCard({
        //   cardList: res.cardList,
        //   success: function (res1) {
        //   }
        // })
      },
      fail:function(res){
        console.log(res);
      }
    })
  }
})
